import os
import subprocess
import sys
import webbrowser
import os
import pytest
from PyQt5.QtCore import QObject
from io import StringIO
import UI

main_url = ''

trigger_select_way = ''
trigger_select = ''

trigger_send_way = ''
trigger_send_message = ''

detect_select_way = ''
detect_select = ''
detect_expected = ''


class MainWindowController(QObject, UI.UI_MainWindow):
    def __init__(self):
        super().__init__()

    def get_connected(self):
        # 自由测试部分
        self.comboBox_trigger1.currentIndexChanged.connect(self.on_trigger1_currentIndexChanged)
        self.comboBox_trigger2.currentIndexChanged.connect(self.on_trigger2_currentIndexChanged)
        self.comboBox_detect.currentIndexChanged.connect(self.on_detect_currentIndexChanged)
        self.pushButton_determine.clicked.connect(self.on_determine_clicked)

        # 性能测试部分
        self.pushButton_performance.clicked.connect(self.on_performance_clicked)
        self.pushButton_checkreports.clicked.connect(self.on_checkreports_clicked)

        # 胡宇桁部分
        self.pushButton_unithyh1.clicked.connect(self.on_unithyh1_clicked)
        self.pushButton_unithyh2.clicked.connect(self.on_unithyh2_clicked)
        self.pushButton_integrationhyh1.clicked.connect(self.on_integrationhyh1_clicked)
        self.pushButton_integrationhyh2.clicked.connect(self.on_integrationhyh2_clicked)
        self.pushButton_combinatorialhyh.clicked.connect(self.on_combinatorialhyh_clicked)

        # 许中洋部分
        self.pushButton_unitxzy1.clicked.connect(self.on_unitxzy1_clicked)
        self.pushButton_unitxzy2.clicked.connect(self.on_unitxzy2_clicked)
        self.pushButton_integrationxzy1.clicked.connect(self.on_integrationxzy1_clicked)
        self.pushButton_integrationxzy2.clicked.connect(self.on_integrationxzy2_clicked)
        self.pushButton_combinatorialxzy.clicked.connect(self.on_combinatorialxzy_clicked)

        # 罗健部分
        self.pushButton_unitlj1.clicked.connect(self.on_unitlj1_clicked)
        self.pushButton_unitlj2.clicked.connect(self.on_unitlj2_clicked)
        self.pushButton_integrationlj1.clicked.connect(self.on_integrationlj1_clicked)
        self.pushButton_integrationlj2.clicked.connect(self.on_integrationlj2_clicked)
        self.pushButton_combinatoriallj.clicked.connect(self.on_combinatoriallj_clicked)

        # 乔崇睿部分
        self.pushButton_unitqcr1.clicked.connect(self.on_unitqcr1_clicked)
        self.pushButton_unitqcr2.clicked.connect(self.on_unitqcr2_clicked)
        self.pushButton_integrationqcr1.clicked.connect(self.on_integrationqcr1_clicked)
        self.pushButton_integrationqcr2.clicked.connect(self.on_integrationqcr2_clicked)
        self.pushButton_combinatorialqcr.clicked.connect(self.on_combinatorialqcr_clicked)

        # 胡菁文部分
        self.pushButton_unithjw1.clicked.connect(self.on_unithjw1_clicked)
        self.pushButton_unithjw2.clicked.connect(self.on_unithjw2_clicked)
        self.pushButton_integrationhjw1.clicked.connect(self.on_integrationhjw1_clicked)
        self.pushButton_integrationhjw2.clicked.connect(self.on_integrationhjw2_clicked)
        self.pushButton_combinatorialhjw.clicked.connect(self.on_combinatorialhjw_clicked)

        # 程一航部分
        self.pushButton_unitcyh1.clicked.connect(self.on_unitcyh1_clicked)
        self.pushButton_unitcyh2.clicked.connect(self.on_unitcyh2_clicked)
        self.pushButton_integrationcyh1.clicked.connect(self.on_integrationcyh1_clicked)
        self.pushButton_integrationcyh2.clicked.connect(self.on_integrationcyh2_clicked)
        self.pushButton_combinatorialcyh.clicked.connect(self.on_combinatorialcyh_clicked)

    def run_test(self, test_case_path):
        self.textBrowser.clear()
        capture_output = StringIO()
        # original_stdout, original_stderr = sys.stdout, sys.stderr # 获取当前输出流，以便恢复
        sys.stdout, sys.stderr = capture_output, capture_output  # 输出流重定向
        sys.argv = ['-sv', '--alluredir', './reports', test_case_path]
        pytest.main(sys.argv)
        captured_text = capture_output.getvalue()  # 捕获输出文本
        # sys.stdout, sys.stderr = original_stdout, original_stderr # 恢复原始的输出流
        self.textBrowser.append(captured_text)  # 将捕获的输出追加到文本中

    # 自由测试部分
    def on_trigger1_currentIndexChanged(self):
        trigger1_text = self.comboBox_trigger1.currentText()
        if trigger1_text == "id":
            self.lineEdit_trigger1.setPlaceholderText("请输入id")

        elif trigger1_text == "xpath":
            self.lineEdit_trigger1.setPlaceholderText("请输入xpath")
        else:
            self.lineEdit_trigger1.setPlaceholderText("请选择搜索方式")

    def on_trigger2_currentIndexChanged(self):
        trigger2_text = self.comboBox_trigger2.currentText()
        if trigger2_text == "输入文字":
            self.lineEdit_trigger2.setReadOnly(False)
            self.lineEdit_trigger2.setStyleSheet("background-color: #FFFFFF")
            self.lineEdit_trigger2.setPlaceholderText("请输入文字")
        else:
            self.lineEdit_trigger2.setReadOnly(True)
            self.lineEdit_trigger2.setStyleSheet("background-color: #CCCCCC")
            self.lineEdit_trigger2.setPlaceholderText("")

    def on_detect_currentIndexChanged(self):
        detect1_text = self.comboBox_detect.currentText()
        if detect1_text == "id":
            self.lineEdit_detect1.setPlaceholderText("请输入id")
            self.lineEdit_detect2.setReadOnly(False)
            self.lineEdit_detect2.setStyleSheet("background-color: #FFFFFF")
            self.lineEdit_detect2.setPlaceholderText("请输入expected内容")
        elif detect1_text == "xpath":
            self.lineEdit_detect1.setPlaceholderText("请输入xpath")
            self.lineEdit_detect2.setReadOnly(False)
            self.lineEdit_detect2.setStyleSheet("background-color: #FFFFFF")
            self.lineEdit_detect2.setPlaceholderText("请输入expected内容")
        elif detect1_text == "url":
            self.lineEdit_detect1.setPlaceholderText("请输入url")
            self.lineEdit_detect2.setReadOnly(True)
            self.lineEdit_detect2.setStyleSheet("background-color: #CCCCCC")
            self.lineEdit_detect2.setPlaceholderText("")
        else:
            self.lineEdit_detect1.setPlaceholderText("请选择搜索方式")
            self.lineEdit_detect2.setReadOnly(False)
            self.lineEdit_detect2.setStyleSheet("background-color: #FFFFFF")
            self.lineEdit_detect2.setPlaceholderText("请输入expected内容")

    def on_determine_clicked(self):
        global main_url

        global trigger_select_way
        global trigger_select

        global trigger_send_way
        global trigger_send_message

        global detect_select_way
        global detect_select
        global detect_expected

        main_url = self.lineEdit_url.text()
        com_trigger1_text = self.comboBox_trigger1.currentText()
        com_trigger2_text = self.comboBox_trigger2.currentText()
        com_detect_text = self.comboBox_detect.currentText()
        line_trigger1_text = self.lineEdit_trigger1.text()
        line_trigger2_text = self.lineEdit_trigger2.text()
        line_detect1_text = self.lineEdit_detect1.text()
        line_detect2_text = self.lineEdit_detect2.text()

        if com_trigger1_text == "id":
            trigger_select_way = "id"
            trigger_select = line_trigger1_text
        elif com_trigger1_text == "xpath":
            trigger_select_way = "xpath"
            trigger_select = line_trigger1_text

        if com_trigger2_text == "点击":
            trigger_send_way = "click"
            trigger_send_message = ""
        elif com_trigger2_text == "输入文字":
            trigger_send_way = "send_message"
            trigger_send_message = line_trigger2_text

        if com_detect_text == "id":
            detect_select_way = "id"
            detect_select = line_detect1_text
            detect_expected = line_detect2_text
        elif com_detect_text == "xpath":
            detect_select_way = "xpath"
            detect_select = line_detect1_text
            detect_expected = line_detect2_text
        elif com_detect_text == "url":
            detect_select_way = "url"
            detect_select = line_detect1_text
            detect_expected = ""
        self.run_test('./test_user_defined.py')

    # 性能测试部分
    # 测试用 勿管
    def on_performance_clicked(self):
        print(f"main_url{main_url}")
        print(f"trigger_select_way{trigger_select_way}")
        print(f"trigger_select_id{trigger_select}")
        print(f"trigger_send_way{trigger_send_way}")
        print(f"trigger_send_message{trigger_send_message}")
        print(f"detect_select_way{detect_select_way}")
        print(f"detect_select_id{detect_select}")
        print(f"detect_expected{detect_expected}")

    def on_checkreports_clicked(self):
        cmd = 'allure generate ./reports -o ./result -c'
        os.system(cmd)
        cmd = 'allure open -h 127.0.0.1 -p 8883 ./result'
        os.system(cmd)

    # 胡宇桁部分
    def on_unithyh1_clicked(self):
        self.run_test('./testcases/hyh/test_unit01_hyh.py')

    def on_unithyh2_clicked(self):
        self.run_test('./testcases/hyh/test_unit02_hyh.py')

    def on_integrationhyh1_clicked(self):
        self.run_test('./testcases/hyh/test_integration01_hyh.py')

    def on_integrationhyh2_clicked(self):
        self.run_test('./testcases/hyh/test_integration02_hyh.py')

    def on_combinatorialhyh_clicked(self):
        self.run_test('./testcases/hyh/test_combinatorial_hyh.py')

    # 许中洋部分
    def on_unitxzy1_clicked(self):
        self.run_test('./testcases/xzy/test_unit01_xzy.py')

    def on_unitxzy2_clicked(self):
        self.run_test('./testcases/xzy/test_unit02_xzy.py')

    def on_integrationxzy1_clicked(self):
        self.run_test('./testcases/xzy/test_integration01_xzy.py')

    def on_integrationxzy2_clicked(self):
        self.run_test('./testcases/xzy/test_integration02_xzy.py')

    def on_combinatorialxzy_clicked(self):
        self.run_test('./testcases/xzy/test_loginCombinations_xzy.py')

    # 罗健部分
    def on_unitlj1_clicked(self):
        self.run_test('./testcases/lj/test_unit01_lj.py')

    def on_unitlj2_clicked(self):
        self.run_test('./testcases/lj/test_unit02_lj.py')

    def on_integrationlj1_clicked(self):
        self.run_test('./testcases/lj/test_intergration01_lj.py')

    def on_integrationlj2_clicked(self):
        self.run_test('./testcases/lj/test_intergration02_lj.py')

    def on_combinatoriallj_clicked(self):
        self.run_test('./testcases/lj/test_combinatorial_lj.py')

    # 乔崇睿部分
    def on_unitqcr1_clicked(self):
        self.run_test('./testcases/qcr/test_unit01_qcr.py')

    def on_unitqcr2_clicked(self):
        self.run_test('./testcases/qcr/test_unit02_qcr.py')

    def on_integrationqcr1_clicked(self):
        self.run_test('./testcases/qcr/test_integration01_qcr.py')

    def on_integrationqcr2_clicked(self):
        self.run_test('./testcases/qcr/test_integration02_qcr.py')

    def on_combinatorialqcr_clicked(self):
        self.run_test('./testcases/qcr/test_combinatorial_qcr.py')

    # 胡菁文部分
    def on_unithjw1_clicked(self):
        self.run_test('testcases/hjw/test_unit01_hjw.py')

    def on_unithjw2_clicked(self):
        self.run_test('testcases/hjw/test_unit02_hjw.py')

    def on_integrationhjw1_clicked(self):
        self.run_test('testcases/hjw/test_integration01_hjw.py')

    def on_integrationhjw2_clicked(self):
        self.run_test('./testcases/hjw/test_integration02_hjw.py')

    def on_combinatorialhjw_clicked(self):
        self.run_test('./testcases/hjw/test_combination_hjw.py')

    # 程一航部分

    def on_unitcyh1_clicked(self):
        self.run_test('testcases/cyh/test_unit01_cyh.py')

    def on_unitcyh2_clicked(self):
        self.run_test('testcases/cyh/test_unit02_cyh.py')

    def on_integrationcyh1_clicked(self):
        self.run_test('./testcases/cyh/test_intrgration01_cyh.py')

    def on_integrationcyh2_clicked(self):
        self.run_test('./testcases/cyh/test_intrgration02_cyh.py')

    def on_combinatorialcyh_clicked(self):
        self.run_test('./testcases/cyh/test_combination_cyh.py')
